package com.jinxin.platform.cdcardpassmgr.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdcardpassmgr.bean.CdcardpassmgrProject;
import com.jinxin.platform.cdcardpassmgr.bean.vo.ProjectTree;
import com.jinxin.platform.cdcardpassmgr.mapper.CdcardpassmgrProjectMapper;
import com.jinxin.platform.cdcardpassmgr.service.ICardpassmgrProjectService;
import com.jinxin.platform.cdcardpassmgr.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-06-30
 * @Author: Lingnan
 **/
@Service
public class CardpassmgrProjectServiceImpl implements ICardpassmgrProjectService {
    @Resource
    private IdWorker idWorker;
    @Autowired
    private CdcardpassmgrProjectMapper projectMapper;

    @Override
    public JsonResult listAll() {
        CdcardpassmgrProject project = projectMapper.selectByKey("1");
        List<ProjectTree> allList = new ArrayList<>();
        if(project != null){
            List<CdcardpassmgrProject> list = projectMapper.list(new HashMap());
            List<ProjectTree> treeList = new ArrayList<>();
            list.forEach(item->{
                ProjectTree projectTree =
                        ProjectTree.builder().id(item.getId()).title(item.getName()).parentId(item.getParentId()).build();
                treeList.add(projectTree);
            });
            for (ProjectTree pDto : treeList) {
                if("0".equals(pDto.getParentId())){
                    findChilds(pDto,treeList);
                    allList.add(pDto);
                }
            }
        }
        return JsonResult.ok("success",allList);
    }

    private void findChilds(ProjectTree root,List<ProjectTree> list){
        List<ProjectTree> childlist=new ArrayList<>();
        //遍历所有数据，找到是入参父节点的子节点的数据，然后加到childlist集合中。
        for (ProjectTree project : list) {
            if (root.getId().equals(project.getParentId()))
                childlist.add(project);
        }
        //若子节点不存在，那么就不必再遍历子节点中的子节点了 直接返回。
        if(childlist.size()==0) return;
        //设置父节点的子节点列表
        root.setChildren(childlist);
        //若子节点存在，接着递归调用该方法，寻找子节点的子节点。
        for (ProjectTree childs : childlist) {
            findChilds(childs, list);
        }
    }

    @Override
    public List<CdcardpassmgrProject> listPage(HashMap param) {
        return projectMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return projectMapper.listPageCount(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("记录Id不存在");
        }
        int result = projectMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult saveOrUpdate(CdcardpassmgrProject project,boolean isUpdate) {
        if (project == null){
            return JsonResult.error("参数错误，操作失败");
        }
        int count;
        String msg = "";
        if(isUpdate){
            count = projectMapper.updateByKey(project);
            msg = "更新成功";
        }else {
            // 根据数据库计算出order
            String parentId = project.getParentId();
            if(!StringUtils.isEmpty(parentId)){
                int n = projectMapper.selectMaxOderById(parentId);
                project.setOrder(n+1);
            }
            count = projectMapper.insert(project);
            msg = "新增成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }
}
